var apiHost = wx.getStorageSync('apiHost');
var page = 0;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    winWidth: 0,
    winHeight: 0,
    indexHeight:0,
    flag :0,
    radio: '',
    userId:'',
    addressList:[]
  },
  /**
     * radio 选择方式
     */
  radioChange: function (e) {
    var that = this;
    var flag = e.currentTarget.dataset.toggle;
    var index = parseInt(e.currentTarget.dataset.index);
    that.setData({
      radio: flag
    });

    var addresList = that.data.addressList[index];

    wx.showLoading({
      title: '跳转中',
      icon: 'loading',
      mask: true
    });
    wx.navigateTo({
      url: '../../../Collage/ordinaryCollage/payment/payment?addresList=' + JSON.stringify(addresList),
      success: function () {
        wx.hideLoading();   //隐藏加载loading
      },
    });

  },
  /**
     * 点击新增
     */
  addClick:function (){
    wx.showLoading({
      title: '跳转中',
      icon: 'loading',
      mask: true
    })

    wx.navigateTo({
      url: '../../personAddress/personNewAddress/personNewAddress',
      successL:function(){
        wx.hideLoading();
      }
    })
  },
  /**
   * 获取列表数据
   */
  loadData:function (){
    var that = this;
    page = 0;
    wx.request({
      url: apiHost + '/hpp/user/address/list' ,
      data:{
        userId: that.data.userId,
        start:page,
        length:'15',
      },
      success:function (res){
        var list = [];
        list = res.data.data;
        if (list){
          that.setData({
            addressList: res.data.data,
          });
        }
      }
    })
    
  },
  /**
   * 点击cell跳转
   */
  itemClick:function(res){

    wx.showLoading({
      title: '跳转中',
      icon:'loading',
    })

    wx.navigateTo({
      url: '../../personAddress/personNewAddress/personNewAddress?addressId='+res.currentTarget.dataset.toggle,
      success :function(){
        wx.hideLoading();
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;

    var flag = options.flag;      //显示radio标识
    var userId = wx.getStorageSync('userId');
    console.log('地址列表的userId',userId);
    if(flag != undefined){
        flag = flag;
    }else{
      flag = '';
    }

    /** 
     * 获取系统信息 
     */
    wx.getSystemInfo({

      success: function (res) {
        that.setData({
          winWidth: res.windowWidth,
          winHeight: res.windowHeight,
          indexHeight: res.windowHeight ,
          userId: userId,
          flag: flag
        });
      }

    });
    that.loadData();
    that.getHeight();
  },
  //获取高度
  getHeight: function () {
    var that = this;
    /*获取指定元素高度*/
    var query3 = wx.createSelectorQuery();
    var query4 = wx.createSelectorQuery();
    var buttonHeight = "";
    var labelHeight = "";

    query3.select('#bottomView').boundingClientRect()
    query3.exec(function (res) {

      buttonHeight = res[0].height;
      wx.getSystemInfo({
        success: function (res) {
          that.setData({
            indexHeight: res.windowHeight - buttonHeight 
          });
        }
      });
    });
    query4.select('#personDataView').boundingClientRect()
    query4.exec(function (res){

      labelHeight = res[0].height;
      that.setData({
        indexHeight: that.data.indexHeight - labelHeight 
      })
    });
  },
  /**
   * 加载更多数据
   */
  loadMoreData:function(){
    var that = this;
    wx.showLoading({
      title: '玩命加载中',
    })
    page = page + 1;
    wx.request({
      url: apiHost + 'hpp/user/address/list',
      data: {
        userId: that.data.userId,
        start: page,
        length: 5,
      },
      success: function (res) {
        var list = that.data.addressList;
        for (var i = 0; i < res.data.data.length; i++) {
          list.push(res.data.data
          [i]);
        }
        //设置数据
        that.setData({
          addressList: list,
        })
        //隐藏加载框
        wx.hideLoading();
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.loadData();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  }
})